package com.ylx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylx.mapper.PxxGoodsMapper;
import com.ylx.model.common.dtos.PageResponseResult;
import com.ylx.model.common.dtos.ResponseResult;
import com.ylx.model.goods.dtos.GoodsPageReqDto;
import com.ylx.model.goods.pojo.PxxGoods;
import com.ylx.service.PxxGoodsService;
import com.ylx.util.ThreadLocalUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author 许文倩
* @description 针对表【pxx_goods】的数据库操作Service实现
* @createDate 2024-06-24 20:26:03
*/
@Service
public class PxxGoodsServiceImpl extends ServiceImpl<PxxGoodsMapper, PxxGoods>
    implements PxxGoodsService{

    @Resource
    PxxGoodsMapper goodsMapper;

    /**
     * 商品别表（分页）
     * @param dto
     * @return
     */
    @Override
    public ResponseResult goodslist(GoodsPageReqDto dto) {
        dto.checkParam();
        // 分页查询
        Page<PxxGoods> page=new Page<>(dto.getPage(),dto.getSize());
        // 构造条件
        LambdaQueryWrapper<PxxGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq( PxxGoods::getId, ThreadLocalUtil.get());
        wrapper.eq(dto.getChannelId()!=null, PxxGoods::getUserId,dto.getChannelId());
        wrapper.eq(dto.getStatus()!=null,PxxGoods::getStatus,dto.getStatus());

        wrapper.orderByDesc(PxxGoods::getId);
        if (dto.getBeginPubDate()!=null&&dto.getEndPubDate()!=null){
            wrapper.between(PxxGoods::getCreateTime,dto.getBeginPubDate(),dto.getEndPubDate());
        }
        page=goodsMapper.selectPage(page,wrapper);
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setCode(200);
        result.setData(page.getRecords());
        result.setErrorMessage("请求成功");
        return result;
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    @Override
    public ResponseResult goodsDel(int id) {
        int i = goodsMapper.deleteById(id);
        return ResponseResult.okResult(i);
    }

    /**
     * 新增商品
     * @param pxxGoods
     * @return
     */
    @Override
    public ResponseResult goodsAdd(PxxGoods pxxGoods) {
        int insert = goodsMapper.insert(pxxGoods);
        return ResponseResult.okResult(insert);
    }

    /**
     * 修改商品
     * @param pxxGoods
     * @return
     */
    @Override
    public ResponseResult goodsUpdate(PxxGoods pxxGoods) {
        int i = goodsMapper.updateById(pxxGoods);
        return ResponseResult.okResult(i);
    }
}




